package com.billdowney.qq.robot.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class U {

    public static Map<String, Object> jsonToMap(String jsonStr) {
        ObjectMapper jsonMapper = new ObjectMapper();
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            // 读取post请求参数
            if (jsonStr != null && jsonStr.length() > 0) {
                result = jsonMapper.readValue(jsonStr,
                        new com.fasterxml.jackson.core.type.TypeReference<Map<String, Object>>() {
                        });
            }
        } catch (Exception e) {
        }
        return result;
    }


    public static String mapToJson(Map<String, Object> map) {
        String json = JSON.toJSONString(map);//map转String
        JSONObject jsonObject = JSON.parseObject(json);//String转json
        return jsonObject.toString();
    }

    public static String mapToJson2(Map<String, Object> map) {
        Set<String> keys = map.keySet();
        String key = "";
        String value = "";
        StringBuffer jsonBuffer = new StringBuffer();
        jsonBuffer.append("{");
        for (Iterator<String> it = keys.iterator(); it.hasNext();) {
            key = (String) it.next();
            value = (String) map.get(key);
            jsonBuffer.append(key + ":" +"\""+ value+"\"");
            if (it.hasNext()) {
                jsonBuffer.append(",");
            }
        }
        jsonBuffer.append("}");
        return jsonBuffer.toString();
    }
















}
